#encoding:utf-8  
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
import itchat
import requests

URL = "http://www.tuling123.com/openapi/api"
man = 0
woman = 0
dontKnow = 0
myFrend = ["唐雪见","刘继尧","大胡子。","Shakespeare"]

def getTalkMessage(msg):
    reqData = {
        'key': '30eaa30fa20a48ee85aea4de79f4fbe9',  # 如果这个Tuling Key不能用，那就换一个
        'info': msg,  # 这是我们发出去的消息
        'userid': 'wechat1',  # 这里你想改什么都可以
    }
    response = requests.post(URL, data=reqData)
    data = response.json()
    print (data)
    return data["text"]

@itchat.msg_register(itchat.content.TEXT)
def text_reply(msg):
    global myFrend
    print (msg.User.NickName + "给你发信息了")
    if msg.User.NickName == "刘继尧":
        if "查看名单" in msg.text:
            return " ".join(myFrend)
    if msg.User.NickName in myFrend:
        msgData = getTalkMessage(msg.text)
        print (msgData)
        return "：" + msgData

def getMyFriens():
    friendList = itchat.get_friends()
    print ("好友列表")
    for val in friendList:
        global man
        global woman
        global dontKnow
        if val.Sex == 1:
            man+=1
        elif val.Sex == 2:
            woman+=1
        else:
            dontKnow += 1
    print ("男 " + str(man))
    print ("女 "+ str(woman))
    print ("未知 "+ str(dontKnow))

def itchatRead():
    print ("正在运行中...")
    getMyFriens()

itchat.auto_login(hotReload=True,loginCallback=itchatRead,enableCmdQR=2)
itchat.run()

getMyFriens()
